package com.xiaotao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaotao.dto.GroupBuyDto;
import com.xiaotao.entity.Goods;
import com.xiaotao.entity.GroupBuy;
import com.xiaotao.entity.Merchant;
import com.xiaotao.mapper.GoodsMapper;
import com.xiaotao.mapper.GroupBuyMapper;
import com.xiaotao.mapper.MerchantMapper;
import com.xiaotao.service.MerchantService;
import com.xiaotao.util.MinioUtil;
import com.xiaotao.util.ResponseEnum;
import com.xiaotao.util.ResponseUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class MerchantServiceImpl implements MerchantService {
    @Resource
    private MerchantMapper merchantMapper;
    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private GroupBuyMapper groupBuyMapper;
    @Resource
    private MinioUtil minioUtil;
    /**
     * 添加商户
     *
     * @param merchant
     * @return
     */
    @Override
    public ResponseUtil save(Merchant merchant) {
        int result = merchantMapper.insert(merchant);
        return result > 0 ? ResponseUtil.get(ResponseEnum.OK) :
                ResponseUtil.get(ResponseEnum.FAIL);
    }


    /**
     * 商户登录
     *
     * @param merchant
     * @return
     */
    @Override
    public ResponseUtil login(Merchant merchant) {
        if (merchant.getUsername().isEmpty() || merchant.getPassword().isEmpty()) {
            return ResponseUtil.get(ResponseEnum.FAIL, "商户用户密码不能为空");
        }
        //根据用户名查询密码
        Merchant merchant1 = merchantMapper.findByUsername(merchant.getUsername());
        if (!merchant.getPassword().equals(merchant1.getPassword())) {
            //密码错误
            return ResponseUtil.get(ResponseEnum.FAIL, "密码或者用户名错误");
        }
        return ResponseUtil.get(ResponseEnum.OK, "登录成功");
    }

    /**
     * 查询所有商户
     *
     * @return
     */
    @Override
    public ResponseUtil findAll(Integer pageNum,Integer pageSize) {
        //分页
        PageHelper.startPage(pageNum,pageSize);
        List<Merchant> list = merchantMapper.findAll();
        PageInfo<Merchant> pageInfo = new PageInfo<>(list);
        return list != null ? ResponseUtil.get(ResponseEnum.OK, pageInfo) :
                ResponseUtil.get(ResponseEnum.FAIL);
    }

    /**
     * 删除商户
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public ResponseUtil delete(Integer id) {
        //物理删除
        Merchant merchant = merchantMapper.selectByPrimaryKey(id);
        if (merchant == null) {
            // 避免merchant为null时调用merchant.getId()导致空指针异常
            return ResponseUtil.get(ResponseEnum.FAIL, "商户不存在");
        }

        //删除与该用户相关联的团购和商品
        List<Goods> list = goodsMapper.findByMerchantId(merchant.getId());
        if (list!=null && !list.isEmpty()){
            goodsMapper.batchDelete(list);
        }
        //根据商户信息找到所有未删除的团购信息
        List<GroupBuyDto> groupBuyList = groupBuyMapper.findByMerchantId(id);
        if (groupBuyList!=null && !groupBuyList.isEmpty()){
            groupBuyMapper.batchDelete(groupBuyList);
        }
        int result = merchantMapper.deleteByPrimaryKey(id);
        return result > 0 ? ResponseUtil.get(ResponseEnum.OK, "删除成功") :
                ResponseUtil.get(ResponseEnum.FAIL, "删除失败");
    }

    /**
     * 根据id查询商户信息
     *
     * @param id
     * @return
     */
    @Override
    public ResponseUtil findById(Integer id) {
        Merchant merchant = merchantMapper.selectByPrimaryKey(id);
        return merchant != null ? ResponseUtil.get(ResponseEnum.OK, merchant) :
                ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil uploadImage(MultipartFile file, String bucketName) throws Exception {
        Map<String, Object> map = minioUtil.uploadFile(file, bucketName);
        String imageUrl = map.get("imageUrl").toString();
        return imageUrl!=null? ResponseUtil.get(ResponseEnum.OK,imageUrl):
                ResponseUtil.get(ResponseEnum.FAIL,"上传文件失败");
    }

    @Override
    public ResponseUtil searchByKeyWord(String keyword,Integer status,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Merchant> merchantList = merchantMapper.searchByKeyWord(keyword,status);
        PageInfo<Merchant> merchantPageInfo = new PageInfo<>(merchantList);
        return merchantList!=null?ResponseUtil.get(ResponseEnum.OK,merchantPageInfo):
                ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil update(Merchant merchant) {
        int result = merchantMapper.updateByPrimaryKeySelective(merchant);
        return result>0?ResponseUtil.get(ResponseEnum.OK,"更新成功"):
                ResponseUtil.get(ResponseEnum.FAIL,"更新失败");
    }

    @Override
    public ResponseUtil findAll2() {
        List<Merchant> list = merchantMapper.findAll();
        return list != null ? ResponseUtil.get(ResponseEnum.OK, list) :
                ResponseUtil.get(ResponseEnum.FAIL);
    }
}
